package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.erp.ErpPda;
import com.nuoniu.sibanyun.mapper.erp.ErpPdaMapper;
import com.nuoniu.sibanyun.service.erp.IErpPdaService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * pda设置 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-12-29
 */
@Service
public class ErpPdaServiceImpl extends ServiceImpl<ErpPdaMapper, ErpPda> implements IErpPdaService {

    @Override
    public Page<ErpPda> pageList(Page page){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("company_id", UserThreadLocal.get().getCompanyId());
        return page(page, queryWrapper);
    }

    @Override
    public void savePda(ErpPda erpPda) {
        UserInfo userInfo = UserThreadLocal.get();
        if(erpPda.getId() == null) {
            erpPda.setCompanyId(userInfo.getCompanyId());
        }

        if(erpPda.getStatus().equals(1)) {
            UpdateWrapper wrapper = new UpdateWrapper();
            wrapper.set("status", 0);
            wrapper.eq("company_id", userInfo.getCompanyId());
            this.update(wrapper);
        }

        this.saveOrUpdate(erpPda);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        ErpPda pda = this.getById(id);
        if(pda != null) {
            pda.setStatus(status);
            this.updateById(pda);
        }
    }

    @Override
    public ErpPda getUsePda() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", UserThreadLocal.get().getCompanyId());
        wrapper.eq("status", 1);
        wrapper.last("LIMIT 1");
        return getOne(wrapper);
    }
}
